<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Query Construction API - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.search.lucene.query-api.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.search.lucene.query-api.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.search.lucene.query-language.html">Query Language</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.search.lucene.html">Zend_Search_Lucene</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.search.lucene.charset.html">Character Set</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.search.lucene.query-api" class="section"><div class="info"><h1 class="title">Query Construction API</h1></div>
    

    <p class="para">
        In addition to parsing a string query automatically it&#039;s also possible to construct them
        with the query <acronym class="acronym">API</acronym>.
    </p>

    <p class="para">
        User queries can be combined with queries created through the query <acronym class="acronym">API</acronym>.
        Simply use the query parser to construct a query from a string:
    </p>

    <pre class="programlisting brush: php">
$query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
</pre>


    <div class="section" id="zend.search.lucene.queries.exceptions"><div class="info"><h1 class="title">Query Parser Exceptions</h1></div>
        

        <p class="para">
            The query parser may generate two types of exceptions:

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <span class="classname">Zend_Search_Lucene_Exception</span> is thrown if something
                        goes wrong in the query parser itself.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <span class="classname">Zend_Search_Lucene_Search_QueryParserException</span> is
                        thrown when there is an error in the query syntax.
                    </p>
                </li>
            </ul>

            It&#039;s a good idea to catch
            <span class="classname">Zend_Search_Lucene_Search_QueryParserException</span>s and handle them
            appropriately:
        </p>

        <pre class="programlisting brush: php">
try {
    $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
} catch (Zend_Search_Lucene_Search_QueryParserException $e) {
    echo &quot;Query syntax error: &quot; . $e-&gt;getMessage() . &quot;\n&quot;;
}
</pre>


        <p class="para">
            The same technique should be used for the find() method of a
            <span class="classname">Zend_Search_Lucene</span> object.
        </p>

        <p class="para">
            Starting in 1.5, query parsing exceptions are suppressed by default. If query doesn&#039;t
            conform query language, then it&#039;s tokenized using current default analyzer and all
            tokenized terms are used for searching. Use
             <span class="methodname">Zend_Search_Lucene_Search_QueryParser::dontSuppressQueryParsingExceptions()</span>
            method to turn exceptions on.
             <span class="methodname">Zend_Search_Lucene_Search_QueryParser::suppressQueryParsingExceptions()</span>
            and
             <span class="methodname">Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed()</span>
            methods are also intended to manage exceptions handling behavior.
        </p>
    </div>

    <div class="section" id="zend.search.lucene.queries.term-query"><div class="info"><h1 class="title">Term Query</h1></div>
        

        <p class="para">
            Term queries can be used for searching with a single term.
        </p>

        <p class="para">
            Query string:
        </p>

        <pre class="programlisting brush: querystring">
word1
</pre>


        <p class="para">or</p>

        <p class="para">
            Query construction by <acronym class="acronym">API</acronym>:
        </p>

        <pre class="programlisting brush: php">
$term  = new Zend_Search_Lucene_Index_Term(&#039;word1&#039;, &#039;field1&#039;);
$query = new Zend_Search_Lucene_Search_Query_Term($term);
$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            The term field is optional. <span class="classname">Zend_Search_Lucene</span> searches through
            all indexed fields in each document if the field is not specified:
        </p>

        <pre class="programlisting brush: php">
// Search for &#039;word1&#039; in all indexed fields
$term  = new Zend_Search_Lucene_Index_Term(&#039;word1&#039;);
$query = new Zend_Search_Lucene_Search_Query_Term($term);
$hits  = $index-&gt;find($query);
</pre>

    </div>

    <div class="section" id="zend.search.lucene.queries.multiterm-query"><div class="info"><h1 class="title">Multi-Term Query</h1></div>
        

        <p class="para">
            Multi-term queries can be used for searching with a set of terms.
        </p>

        <p class="para">
            Each term in a set can be defined as <em class="emphasis">required</em>,
            <em class="emphasis">prohibited</em>, or <em class="emphasis">neither</em>.

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">required</em> means that documents not matching this term
                        will not match the query;
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">prohibited</em> means that documents matching this term will
                        not match the query;
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">neither</em>, in which case matched documents are neither
                        prohibited from, nor required to, match the term. A document must match at
                        least 1 term, however, to match the query.
                    </p>
                </li>
            </ul>
        </p>

        <p class="para">
            If optional terms are added to a query with required terms, both queries will have the
            same result set but the optional terms may affect the score of the matched documents.
        </p>

        <p class="para">
            Both search methods can be used for multi-term queries.
        </p>

        <p class="para">
            Query string:
        </p>

        <pre class="programlisting brush: querystring">
+word1 author:word2 -word3
</pre>


        <ul class="itemizedlist">
            <li class="listitem"><p class="para">&#039;+&#039; is used to define a required term.</p></li>
            <li class="listitem"><p class="para">&#039;-&#039; is used to define a prohibited term.</p></li>

            <li class="listitem">
                <p class="para">
                    &#039;field:&#039; prefix is used to indicate a document field for a search.
                    If it&#039;s omitted, then all fields are searched.
                </p>
            </li>
        </ul>

        <p class="para">or</p>

        <p class="para">
            Query construction by <acronym class="acronym">API</acronym>:
        </p>

        <pre class="programlisting brush: php">
$query = new Zend_Search_Lucene_Search_Query_MultiTerm();

$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word1&#039;), true);
$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word2&#039;, &#039;author&#039;),
                null);
$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word3&#039;), false);

$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            It&#039;s also possible to specify terms list within MultiTerm query constructor:
        </p>

        <pre class="programlisting brush: php">
$terms = array(new Zend_Search_Lucene_Index_Term(&#039;word1&#039;),
               new Zend_Search_Lucene_Index_Term(&#039;word2&#039;, &#039;author&#039;),
               new Zend_Search_Lucene_Index_Term(&#039;word3&#039;));
$signs = array(true, null, false);

$query = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $signs);

$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            The <var class="varname">$signs</var> array contains information about the term type:

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <b><tt>TRUE</tt></b> is used to define required term.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>FALSE</tt></b> is used to define prohibited term.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>NULL</tt></b> is used to define a term that is neither required
                        nor prohibited.
                    </p>
                </li>
            </ul>
        </p>
    </div>

    <div class="section" id="zend.search.lucene.queries.boolean-query"><div class="info"><h1 class="title">Boolean Query</h1></div>
        

        <p class="para">
            Boolean queries allow to construct query using other queries and boolean operators.
        </p>

        <p class="para">
            Each subquery in a set can be defined as <em class="emphasis">required</em>,
            <em class="emphasis">prohibited</em>, or <em class="emphasis">optional</em>.

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">required</em> means that documents not matching this
                        subquery will not match the query;
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">prohibited</em> means that documents matching this subquery
                        will not match the query;
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis">optional</em>, in which case matched documents are neither
                        prohibited from, nor required to, match the subquery. A document must match
                        at least 1 subquery, however, to match the query.
                    </p>
                </li>
            </ul>
        </p>

        <p class="para">
            If optional subqueries are added to a query with required subqueries, both queries will
            have the same result set but the optional subqueries may affect the score of the matched
            documents.
        </p>

        <p class="para">
            Both search methods can be used for boolean queries.
        </p>

        <p class="para">
            Query string:
        </p>

        <pre class="programlisting brush: querystring">
+(word1 word2 word3) (author:word4 author:word5) -(word6)
</pre>


        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    &#039;+&#039; is used to define a required subquery.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    &#039;-&#039; is used to define a prohibited subquery.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    &#039;field:&#039; prefix is used to indicate a document field for a search.
                    If it&#039;s omitted, then all fields are searched.
                </p>
            </li>
        </ul>

        <p class="para">or</p>

        <p class="para">
            Query construction by <acronym class="acronym">API</acronym>:
        </p>

        <pre class="programlisting brush: php">
$query = new Zend_Search_Lucene_Search_Query_Boolean();

$subquery1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
$subquery1-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word1&#039;));
$subquery1-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word2&#039;));
$subquery1-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word3&#039;));

$subquery2 = new Zend_Search_Lucene_Search_Query_MultiTerm();
$subquery2-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word4&#039;, &#039;author&#039;));
$subquery2-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word5&#039;, &#039;author&#039;));

$term6 = new Zend_Search_Lucene_Index_Term(&#039;word6&#039;);
$subquery3 = new Zend_Search_Lucene_Search_Query_Term($term6);

$query-&gt;addSubquery($subquery1, true  /* required */);
$query-&gt;addSubquery($subquery2, null  /* optional */);
$query-&gt;addSubquery($subquery3, false /* prohibited */);

$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            It&#039;s also possible to specify subqueries list within Boolean query constructor:
        </p>

        <pre class="programlisting brush: php">
...
$subqueries = array($subquery1, $subquery2, $subquery3);
$signs = array(true, null, false);

$query = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);

$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            The <var class="varname">$signs</var> array contains information about the subquery type:

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <b><tt>TRUE</tt></b> is used to define required subquery.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>FALSE</tt></b> is used to define prohibited subquery.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <b><tt>NULL</tt></b> is used to define a subquery that is neither
                        required nor prohibited.
                    </p>
                </li>
            </ul>
        </p>

        <p class="para">
            Each query which uses boolean operators can be rewritten using signs notation and
            constructed using <acronym class="acronym">API</acronym>. For example:
        </p>

        <pre class="programlisting brush: querystring">
word1 AND (word2 AND word3 AND NOT word4) OR word5
</pre>


        <p class="para">
            is equivalent to
        </p>

        <pre class="programlisting brush: querystring">
(+(word1) +(+word2 +word3 -word4)) (word5)
</pre>

    </div>

    <div class="section" id="zend.search.lucene.queries.wildcard"><div class="info"><h1 class="title">Wildcard Query</h1></div>
        

        <p class="para">
            Wildcard queries can be used to search for documents containing strings matching
            specified patterns.
        </p>

        <p class="para">
            The &#039;?&#039; symbol is used as a single character wildcard.
        </p>

        <p class="para">
            The &#039;*&#039; symbol is used as a multiple character wildcard.
        </p>

        <p class="para">
            Query string:
        </p>

        <pre class="programlisting brush: querystring">
field1:test*
</pre>


        <p class="para">or</p>

        <p class="para">
            Query construction by <acronym class="acronym">API</acronym>:
        </p>

        <pre class="programlisting brush: php">
$pattern = new Zend_Search_Lucene_Index_Term(&#039;test*&#039;, &#039;field1&#039;);
$query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            The term field is optional. <span class="classname">Zend_Search_Lucene</span> searches through
            all fields on each document if a field is not specified:
        </p>

        <pre class="programlisting brush: php">
$pattern = new Zend_Search_Lucene_Index_Term(&#039;test*&#039;);
$query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
$hits  = $index-&gt;find($query);
</pre>

    </div>

    <div class="section" id="zend.search.lucene.queries.fuzzy"><div class="info"><h1 class="title">Fuzzy Query</h1></div>
        

        <p class="para">
            Fuzzy queries can be used to search for documents containing strings matching terms
            similar to specified term.
        </p>

        <p class="para">
            Query string:
        </p>

        <pre class="programlisting brush: querystring">
field1:test~
</pre>


        <p class="para">
            This query matches documents containing &#039;test&#039; &#039;text&#039; &#039;best&#039; words and others.
        </p>

        <p class="para">or</p>

        <p class="para">
            Query construction by <acronym class="acronym">API</acronym>:
        </p>

        <pre class="programlisting brush: php">
$term = new Zend_Search_Lucene_Index_Term(&#039;test&#039;, &#039;field1&#039;);
$query = new Zend_Search_Lucene_Search_Query_Fuzzy($term);
$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            Optional similarity can be specified after &quot;~&quot; sign.
        </p>

        <p class="para">
            Query string:
        </p>

        <pre class="programlisting brush: querystring">
field1:test~0.4
</pre>


        <p class="para">or</p>

        <p class="para">
            Query construction by <acronym class="acronym">API</acronym>:
        </p>

        <pre class="programlisting brush: php">
$term = new Zend_Search_Lucene_Index_Term(&#039;test&#039;, &#039;field1&#039;);
$query = new Zend_Search_Lucene_Search_Query_Fuzzy($term, 0.4);
$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            The term field is optional. <span class="classname">Zend_Search_Lucene</span> searches through
            all fields on each document if a field is not specified:
        </p>

        <pre class="programlisting brush: php">
$term = new Zend_Search_Lucene_Index_Term(&#039;test&#039;);
$query = new Zend_Search_Lucene_Search_Query_Fuzzy($term);
$hits  = $index-&gt;find($query);
</pre>

    </div>

    <div class="section" id="zend.search.lucene.queries.phrase-query"><div class="info"><h1 class="title">Phrase Query</h1></div>
        

        <p class="para">
            Phrase Queries can be used to search for a phrase within documents.
        </p>

        <p class="para">
            Phrase Queries are very flexible and allow the user or developer to search for exact
            phrases as well as &#039;sloppy&#039; phrases.
        </p>

        <p class="para">
            Phrases can also contain gaps or terms in the same places; they can be generated by
            the analyzer for different purposes. For example, a term can be duplicated to increase
            the term its weight, or several synonyms can be placed into a single position.
        </p>

        <pre class="programlisting brush: php">
$query1 = new Zend_Search_Lucene_Search_Query_Phrase();

// Add &#039;word1&#039; at 0 relative position.
$query1-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word1&#039;));

// Add &#039;word2&#039; at 1 relative position.
$query1-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word2&#039;));

// Add &#039;word3&#039; at 3 relative position.
$query1-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;word3&#039;), 3);

...

$query2 = new Zend_Search_Lucene_Search_Query_Phrase(
                array(&#039;word1&#039;, &#039;word2&#039;, &#039;word3&#039;), array(0,1,3));

...

// Query without a gap.
$query3 = new Zend_Search_Lucene_Search_Query_Phrase(
                array(&#039;word1&#039;, &#039;word2&#039;, &#039;word3&#039;));

...

$query4 = new Zend_Search_Lucene_Search_Query_Phrase(
                array(&#039;word1&#039;, &#039;word2&#039;), array(0,1), &#039;annotation&#039;);
</pre>


        <p class="para">
            A phrase query can be constructed in one step with a class constructor or step by step
            with  <span class="methodname">Zend_Search_Lucene_Search_Query_Phrase::addTerm()</span> method
            calls.
        </p>

        <p class="para">
            <span class="classname">Zend_Search_Lucene_Search_Query_Phrase</span> class constructor takes
            three optional arguments:
        </p>

        <pre class="programlisting brush: php">
Zend_Search_Lucene_Search_Query_Phrase(
    [array $terms[, array $offsets[, string $field]]]
);
</pre>


        <p class="para">
            The <var class="varname">$terms</var> parameter is an array of strings that contains a set of
            phrase terms. If it&#039;s omitted or equal to <b><tt>NULL</tt></b>, then an empty query
            is constructed.
        </p>

        <p class="para">
            The <var class="varname">$offsets</var> parameter is an array of integers that contains offsets
            of terms in a phrase. If it&#039;s omitted or equal to <b><tt>NULL</tt></b>, then the
            terms&#039; positions are assumed to be sequential with no gaps.
        </p>

        <p class="para">
            The <var class="varname">$field</var> parameter is a string that indicates the document field
            to search. If it&#039;s omitted or equal to <b><tt>NULL</tt></b>, then the default field
            is searched.
        </p>

        <p class="para">
            Thus:
        </p>

        <pre class="programlisting brush: php">
$query =
    new Zend_Search_Lucene_Search_Query_Phrase(array(&#039;zend&#039;, &#039;framework&#039;));
</pre>


        <p class="para">
            will search for the phrase &#039;zend framework&#039; in all fields.
        </p>

        <pre class="programlisting brush: php">
$query = new Zend_Search_Lucene_Search_Query_Phrase(
                 array(&#039;zend&#039;, &#039;download&#039;), array(0, 2)
             );
</pre>


        <p class="para">
            will search for the phrase &#039;zend ????? download&#039; and match &#039;zend platform download&#039;,
            &#039;zend studio download&#039;, &#039;zend core download&#039;, &#039;zend framework download&#039;, and so on.
        </p>

        <pre class="programlisting brush: php">
$query = new Zend_Search_Lucene_Search_Query_Phrase(
                 array(&#039;zend&#039;, &#039;framework&#039;), null, &#039;title&#039;
             );
</pre>


        <p class="para">
            will search for the phrase &#039;zend framework&#039; in the &#039;title&#039; field.
        </p>

        <p class="para">
             <span class="methodname">Zend_Search_Lucene_Search_Query_Phrase::addTerm()</span> takes two
            arguments, a required <span class="classname">Zend_Search_Lucene_Index_Term</span> object and an
            optional position:
        </p>

        <pre class="programlisting brush: php">
Zend_Search_Lucene_Search_Query_Phrase::addTerm(
    Zend_Search_Lucene_Index_Term $term[, integer $position]
);
</pre>


        <p class="para">
            The <var class="varname">$term</var> parameter describes the next term in the phrase. It must
            indicate the same field as previous terms, or an exception will be thrown.
        </p>

        <p class="para">
            The <var class="varname">$position</var> parameter indicates the term position in the phrase.
        </p>

        <p class="para">
            Thus:
        </p>

        <pre class="programlisting brush: php">
$query = new Zend_Search_Lucene_Search_Query_Phrase();
$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;zend&#039;));
$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;framework&#039;));
</pre>


        <p class="para">
            will search for the phrase &#039;zend framework&#039;.
        </p>

        <pre class="programlisting brush: php">
$query = new Zend_Search_Lucene_Search_Query_Phrase();
$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;zend&#039;), 0);
$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;framework&#039;), 2);
</pre>


        <p class="para">
            will search for the phrase &#039;zend ????? download&#039; and match &#039;zend platform download&#039;,
            &#039;zend studio download&#039;, &#039;zend core download&#039;, &#039;zend framework download&#039;, and so on.
        </p>

        <pre class="programlisting brush: php">
$query = new Zend_Search_Lucene_Search_Query_Phrase();
$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;zend&#039;, &#039;title&#039;));
$query-&gt;addTerm(new Zend_Search_Lucene_Index_Term(&#039;framework&#039;, &#039;title&#039;));
</pre>


        <p class="para">
            will search for the phrase &#039;zend framework&#039; in the &#039;title&#039; field.
        </p>

        <p class="para">
            The slop factor sets the number of other words permitted between specified words in the
            query phrase. If set to zero, then the corresponding query is an exact phrase search.
            For larger values this works like the WITHIN or NEAR operators.
        </p>

        <p class="para">
            The slop factor is in fact an edit distance, where the edits correspond to moving terms
            in the query phrase. For example, to switch the order of two words requires two moves
            (the first move places the words atop one another), so to permit re-orderings of
            phrases, the slop factor must be at least two.
        </p>

        <p class="para">
            More exact matches are scored higher than sloppier matches; thus, search results are
            sorted by exactness. The slop is zero by default, requiring exact matches.
        </p>

        <p class="para">
            The slop factor can be assigned after query creation:
        </p>

        <pre class="programlisting brush: php">
// Query without a gap.
$query =
    new Zend_Search_Lucene_Search_Query_Phrase(array(&#039;word1&#039;, &#039;word2&#039;));

// Search for &#039;word1 word2&#039;, &#039;word1 ... word2&#039;
$query-&gt;setSlop(1);
$hits1 = $index-&gt;find($query);

// Search for &#039;word1 word2&#039;, &#039;word1 ... word2&#039;,
// &#039;word1 ... ... word2&#039;, &#039;word2 word1&#039;
$query-&gt;setSlop(2);
$hits2 = $index-&gt;find($query);
</pre>

    </div>

    <div class="section" id="zend.search.lucene.queries.range"><div class="info"><h1 class="title">Range Query</h1></div>
        

        <p class="para">
            <a href="zend.search.lucene.query-language.html#zend.search.lucene.query-language.range" class="link">Range queries</a> are
            intended for searching terms within specified interval.
        </p>

        <p class="para">
            Query string:
        </p>

        <pre class="programlisting brush: querystring">
mod_date:[20020101 TO 20030101]
title:{Aida TO Carmen}
</pre>


        <p class="para">or</p>

        <p class="para">
            Query construction by <acronym class="acronym">API</acronym>:
        </p>

        <pre class="programlisting brush: php">
$from = new Zend_Search_Lucene_Index_Term(&#039;20020101&#039;, &#039;mod_date&#039;);
$to   = new Zend_Search_Lucene_Index_Term(&#039;20030101&#039;, &#039;mod_date&#039;);
$query = new Zend_Search_Lucene_Search_Query_Range(
                 $from, $to, true // inclusive
             );
$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            Term fields are optional. <span class="classname">Zend_Search_Lucene</span> searches through all
            fields if the field is not specified:
        </p>

        <pre class="programlisting brush: php">
$from = new Zend_Search_Lucene_Index_Term(&#039;Aida&#039;);
$to   = new Zend_Search_Lucene_Index_Term(&#039;Carmen&#039;);
$query = new Zend_Search_Lucene_Search_Query_Range(
                 $from, $to, false // non-inclusive
             );
$hits  = $index-&gt;find($query);
</pre>


        <p class="para">
            Either (but not both) of the boundary terms may be set to <b><tt>NULL</tt></b>.
            <span class="classname">Zend_Search_Lucene</span> searches from the beginning or
            up to the end of the dictionary for the specified field(s) in this case:
        </p>

        <pre class="programlisting brush: php">
// searches for [&#039;20020101&#039; TO ...]
$from = new Zend_Search_Lucene_Index_Term(&#039;20020101&#039;, &#039;mod_date&#039;);
$query = new Zend_Search_Lucene_Search_Query_Range(
                 $from, null, true // inclusive
             );
$hits  = $index-&gt;find($query);
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.search.lucene.query-language.html">Query Language</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.search.lucene.html">Zend_Search_Lucene</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.search.lucene.charset.html">Character Set</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.search.lucene.html">Zend_Search_Lucene</a></li>
  <li><a href="zend.search.lucene.overview.html">Overview</a></li>
  <li><a href="zend.search.lucene.index-creation.html">Building Indexes</a></li>
  <li><a href="zend.search.lucene.searching.html">Searching an Index</a></li>
  <li><a href="zend.search.lucene.query-language.html">Query Language</a></li>
  <li class="active"><a href="zend.search.lucene.query-api.html">Query Construction API</a></li>
  <li><a href="zend.search.lucene.charset.html">Character Set</a></li>
  <li><a href="zend.search.lucene.extending.html">Extensibility</a></li>
  <li><a href="zend.search.lucene.java-lucene.html">Interoperating with Java Lucene</a></li>
  <li><a href="zend.search.lucene.advanced.html">Advanced</a></li>
  <li><a href="zend.search.lucene.best-practice.html">Best Practices</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>